package com.itheima.CSM_bacK.aaaControllers;

//import com.itheima.CSM.aabServices.Serv.OrderSettingServ;
import com.itheima.CSM_bacK.aabServices.Serv.OrderSettingServ;
import com.itheima.common.entity.Result;
import com.itheima.pojo.DTO.DTOoSettingEdit;
import com.itheima.pojo.VO.VOoSettingGet;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;
import java.util.Locale;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {
    @Autowired
    private OrderSettingServ orderSettingServ;


    @GetMapping("/download")
    public String download(String filename, HttpServletResponse response) throws Exception {
        String path = "templates/" + filename;
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // 设置为 Excel 文件类型
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8")); // 设置为附件下载，并指定文件名
        InputStream is = getClass().getClassLoader().getResourceAsStream(path);
        IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();

        return "文件下载";

    }

    @PostMapping("/editNumberByOrderDate")
    public Result editNumberByOrderDate(@RequestBody DTOoSettingEdit kk) {
        orderSettingServ.editNumberByOrderDate(kk);
        return new Result(true, "");
    }


    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String month) {
        List<VOoSettingGet> kks = orderSettingServ.getOrderSettingByMonth(month);
        return new Result(true, "", kks);
    }

    @PostMapping("/upload")
    public Result upload(MultipartFile excelFile, Locale locale) throws Exception {
        //读
        FileInputStream fis = (FileInputStream) excelFile.getInputStream();
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 1; i <= lastRowNum; i++) {
            XSSFRow row = sheet.getRow(i);
            Cell cell1 = row.getCell(0);
            Cell cell2 = row.getCell(1);
            Date date = cell1.getDateCellValue();
            int number = (int) cell2.getNumericCellValue();
            DTOoSettingEdit kk = new DTOoSettingEdit();
//            LocalDate localDate = LocalDate.ofInstant(date.toInstant(), ZoneId.systemDefault());
//
//            kk.setOrderDate(localDate);kk.setNumber(number);
            orderSettingServ.editNumberByOrderDate(kk);
        }

        return new Result(true, "预约设置成功");
    }
}
